<template>
  <view>
    <button @click="handleDownload">下载文件</button>
    <button @click="uploadfile">上传文件</button>
    <button @click="selectFile">选择文件上传</button>
    <view v-if="downloading">下载进度: {{ progress }}%</view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      downloading: false,
      progress: 0
    };
  },
  methods: {
    // todo: 待验证app上为什么不行
    selectFile() {
      uni.chooseFile({
        success: (chooseRes) => {
          const tempFilePath = chooseRes.tempFilePaths[0];
          // 立即上传，不要延迟
          uni.uploadFile({
            url: 'http://192.168.100.101:3001/upload',
            filePath: tempFilePath,
            name: 'file',
            success: (uploadRes) => {
              console.log('上传成功：', uploadRes);
            }
          });
        }
      });
    },
    uploadfile() {
      console.log('开始上传文件！');
      let res = {
        // tempFilePath: '/storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/doc/uniapp_save/17417555963560.zip'
        tempFilePath: '/storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/doc/uniapp_save/17466864232990.docx'
      };

      uni.uploadFile({
        // url: 'http://192.168.43.209:3000/upload',
        url: 'http://192.168.100.101:3001/upload',
        filePath: res.tempFilePath,
        name: 'file',
        success: (uploadRes) => {
          console.log(31, uploadRes);
          /* uploadRes
          {
            "data": "{   
              "message":"文件上传成功",
              "file":{
                "filename":"17417555963560-1741760229633.zip",
                "path":"/Users/guokun/mayun/nodegroup/express/uploads/17417555963560-1741760229633.zip",
                "size":38881
              }
            }",
            "statusCode": 200,
            "errMsg": "uploadFile:ok"
          }
          */
          uni.showToast({
            title: '文件已上传到电脑',
            icon: 'success'
          });
        },
        fail: (err) => {
          console.error('上传失败：', err);
        }
      });
    },
    handleDownload() {
      this.downloading = true;
      this.progress = 0;

      const downloadTask = uni.downloadFile({
        url: 'http://172.16.9.205:3000/downloadFile',
        success: (res) => {
          console.log(26, res);
          /* res
          {
            "tempFilePath": "_doc/uniapp_temp_1741758470755/download/files.zip",
            "statusCode": 200,
            "errMsg": "downloadFile:ok"
          }
          */

          // 获取实际路径
          const realPath = plus.io.convertLocalFileSystemURL(res.tempFilePath);
          // /storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/doc/uniapp_temp_1741759044964/download/files.zip
          console.log('实际文件路径：', realPath);

          if (res.statusCode === 200) {
            // 保存文件到本地
            uni.saveFile({
              tempFilePath: res.tempFilePath,
              success: (saveRes) => {
                console.log(36, saveRes);
                /* saveRes
                {
                    "savedFilePath": "_doc/uniapp_save/17417584792800.zip",
                    "errMsg": "saveFile:ok"
                }
                */
                uni.showToast({
                  title: '文件已保存',
                  icon: 'success'
                });
                // 打开文件
                // uni.openDocument({
                //   filePath: saveRes.savedFilePath,
                //   success: () => {
                //     console.log('文件打开成功');
                //   },
                //   fail: (err) => {
                //     console.error('打开文件失败', err);
                //   }
                // });
              },
              fail: (err) => {
                uni.showToast({
                  title: '保存失败',
                  icon: 'none'
                });
                console.error('保存文件失败:', err);
              }
            });
          }
        },
        fail: (err) => {
          uni.showToast({
            title: '下载失败',
            icon: 'none'
          });
          console.error('下载失败:', err);
        },
        complete: () => {
          this.downloading = false;
        }
      });

      // 监听下载进度
      downloadTask.onProgressUpdate((res) => {
        this.progress = res.progress;
      });
    }
  }
};
</script>

<style>
button {
  margin: 20px;
  padding: 10px 20px;
}
</style>
